package com.arron.algorithm.leetcodetop100.双指针.n数之和;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * leetcode 1. 两数之和
 */
public class 两数之和 {


    public static void main(String[] args) {

        int nums[] = {2,7,11,15,6,3,8,1};
        int target = 9;
        twoSum2(nums,target);
    }


    /**
     * 暴力破解
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {

        List<Integer> res = new ArrayList<>();
        for (int i = 0; i < nums.length-1; i++) {
            for (int j = i+1; j <  nums.length; j++) {
                if (nums[i]+nums[j] == target){
                    res.add(i);
                    res.add(j);
                }
            }
        }

        return res.stream().mapToInt(v->v).toArray();
    }


    //
    public static void twoSum2(int[] nums, int target) {

        Map<Integer, Integer> tempIntsMap = new HashMap<>();


        for (int i = 0; i < nums.length; i++) {
            if (tempIntsMap.containsKey(target- nums[i])){
                System.out.println(tempIntsMap.get(target- nums[i])+","+i);
                break;
            }else {
                tempIntsMap.put(nums[i],i);
            }

        }


    }


}
